package com.core.service.Impl;

import com.core.dao.DjsDao;
import com.core.domain.UserParse;
import com.core.service.DjsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @program: JSPStudy
 * @description:
 * @author: djs
 * @create: 2023-04-27 16:49
 **/

@Service
public class DjsServiceImpl implements DjsService {
    @Autowired
    private DjsDao DjsDao;

    /**
     * 设计思路：首先从场馆表查询出所有场馆的id，通过id查询出对应的name，随后查询出每种场馆的租用价格，再从中间表中查出预约的每种场馆的数量，两者相乘得出价格，随后组成键值对加入map中
     *
     * @return
     */
    @Override
    public Map<String, Integer> finalMoney(String startTime, String endTime) {
        Map<String, Integer> eachTeamMoney = new LinkedHashMap<>();
        //从场馆表查询出所有场馆的id
        List<Integer> teamIds = selectTeamId();
        for (Integer teamId : teamIds) {
            Integer money = 0;
            //通过id查询出对应的name
            String teamName = selectTeamNameByTeamId(teamId);
            Integer count = selectCountOfTeam(teamId, startTime, endTime) ;
            Integer moneyOfEach = selectMoneyOfTeam(teamId);
            money = moneyOfEach * count;
            eachTeamMoney.put(teamName, money);
        }
        return eachTeamMoney;
    }

    public List<Integer> selectTeamId() {
        List<Integer> teamId = DjsDao.selectTeamId();
        return teamId;
    }

    private String selectTeamNameByTeamId(Integer teamId) {
        String teamName = DjsDao.selectTeamNameByTeamId(teamId);
        return teamName;
    }

    public Integer selectCountOfTeam(Integer teamId, String startTime, String endTime) {
        Integer count = DjsDao.selectCountOfTeam(teamId, startTime, endTime);
        return count;
    }

    public Integer selectMoneyOfTeam(Integer teamId) {
        Integer money = DjsDao.selectMoneyOfTeam(teamId);
        return money;
    }

    /**
     * 设计思路：首先从场馆表查询出所有场馆的id，通过id查询出对应的name，再从中间表中查询出每种场馆预约的用户数量。
     *
     * @return
     */
    @Override
    public Map<String, Integer> getUserNumbers(String startTime, String endTime) {
        Map<String, Integer> eachTeamUserNumbers = new LinkedHashMap<>();
        List<Integer> teamIds = selectTeamId();
        for (Integer teamId : teamIds) {
            Integer userNumber = 0;
            String teamName = selectTeamNameByTeamId(teamId);
            userNumber = DjsDao.selectCountOfTeam(teamId, startTime, endTime);
            eachTeamUserNumbers.put(teamName, userNumber);
        }
        return eachTeamUserNumbers;
    }

    /**
     * 设计思路：查出每个场馆的id，然后根据场馆id先从中间表查出每种场馆预约人的数量，随后再根据场馆表查出每种场馆的数量，最后计算利用率
     *
     * @return
     */
    @Override
    public Map<String, String> getUtilizationRate(String startTime, String endTime) {
        Map<String, String> eachTeamUtilizationRate = new LinkedHashMap<>();
        List<Integer> teamIds = selectTeamId();
        for (Integer teamId : teamIds) {
            Integer teamNumber = DjsDao.selectEachTeamNumber(teamId);
            Integer userNumber = DjsDao.selectCountOfTeam(teamId, startTime, endTime);
            String teamName = selectTeamNameByTeamId(teamId);
            double UtilizationRate = userNumber.doubleValue() / teamNumber.doubleValue();
            eachTeamUtilizationRate.put(teamName, String.format("%.2f", UtilizationRate * 100) + "%");
        }
        return eachTeamUtilizationRate;
    }

    @Override
    public List<Map<String, Object>> getPopular(String startTime, String endTime) {
        List<Map<String, Object>> eachTeamPopular = new ArrayList<>();
        List<Integer> teamIds = selectTeamId();
        for (Integer teamId : teamIds) {
            Map<String, Object> data = new LinkedHashMap<>();
            String teamName = selectTeamNameByTeamId(teamId);
            data.put("teamName", teamName);
            //  查询出每一个体育场所的女性预定总人数以及好评人数
            Integer eachTeamOfGenderAllNumbers = DjsDao.selectTeamOfAllNumbers(teamName, 1, startTime, endTime);
            Integer eachTeamOfGenderPopularNumbers = DjsDao.selectTeamOfPopularNumbers(teamName, 1, startTime, endTime);
            //  查询出每一个体育场所的男性预定总人数以及好评人数
            Integer eachTeamOfUnGenderAllNumbers = DjsDao.selectTeamOfAllNumbers(teamName, 0, startTime, endTime);
            Integer eachTeamOfUnGenderPopularNumbers = DjsDao.selectTeamOfPopularNumbers(teamName, 0, startTime, endTime);
            if (eachTeamOfGenderAllNumbers == 0 && eachTeamOfUnGenderAllNumbers != 0) {
                data.put("maleFavorableRating", (String.format("%.2f", popular(eachTeamOfUnGenderPopularNumbers, eachTeamOfUnGenderAllNumbers) * 100) + "%"));
                data.put("femaleFavorableRating", "无人预定");
            } else if (eachTeamOfGenderAllNumbers != 0 && eachTeamOfUnGenderAllNumbers == 0) {
                data.put("maleFavorableRating", "无人预定");
                data.put("femaleFavorableRating", (String.format("%.2f", popular(eachTeamOfGenderPopularNumbers, eachTeamOfGenderAllNumbers) * 100) + "%"));
            } else if (eachTeamOfGenderAllNumbers == 0 && eachTeamOfUnGenderAllNumbers == 0) {
                data.put("maleFavorableRating", "无人预定");
                data.put("femaleFavorableRating", "无人预定");
            } else {
                data.put("maleFavorableRating", (String.format("%.2f", popular(eachTeamOfUnGenderPopularNumbers, eachTeamOfUnGenderAllNumbers) * 100) + "%"));
                data.put("femaleFavorableRating", (String.format("%.2f", popular(eachTeamOfUnGenderPopularNumbers, eachTeamOfUnGenderAllNumbers) * 100) + "%"));
            }
            eachTeamPopular.add(data);
        }
        return eachTeamPopular;
    }

    @Override
    public List<Map<String, Object>> getAge(String startTime, String endTime) {
        List<Map<String, Object>> eachTeamAgePopular = new ArrayList<>();
        List<Integer> teamIds = selectTeamId();
        String pre = "Age";
        for (Integer teamId : teamIds) {
            Map<String, Object> data = new LinkedHashMap<>();
            String teamName = selectTeamNameByTeamId(teamId);
            data.put("teamName", teamName);
            Integer startAge = 0;
            for (Integer i = startAge; i < 100; i += 10) {
                Integer endAge = i + 10;
                Integer j = i + 1;
                //  查询各体育场各年龄段总人数
                Integer eachTeamEachAllAgeNumbers = DjsDao.selectEachTeamEachAllAgeNumbers(teamName, j, endAge, startTime, endTime);
                //  查询各体育场各年龄段好评人数
                Integer eachTeamEachAgeNumbers = DjsDao.selectEachTeamEachAgeNumbers(teamName, j, endAge, 0, startTime, endTime);
                if (eachTeamEachAllAgeNumbers != 0) {
                    data.put(pre + j, String.format("%.2f", popular(eachTeamEachAgeNumbers, eachTeamEachAllAgeNumbers) * 100) + "%");
                } else {
                    data.put(pre + j, "无人预定");
                }
            }
            eachTeamAgePopular.add(data);
        }
        return eachTeamAgePopular;
    }

    @Override
    public List<Map<String,String>> getPrevious(Object userId) {
        List<Map<String,String>> data = new LinkedList<>();
        LocalDateTime nowTime = LocalDateTime.now();
        List<UserParse> dataList = DjsDao.selectUserParse(userId,nowTime);
        for (UserParse userParse : dataList) {
            Map<String,String> map = new LinkedHashMap<>();
            map.put("id", userParse.getId());
            map.put("userAccount", userParse.getUserAccount());
            map.put("stadiumName", userParse.getStadiumName());
            map.put("startTime", userParse.getStartTime());
            map.put("endTime", userParse.getEndTime());
            if (userParse.getFantastic() == 0) {
                map.put("fantastic", "优");
            } else {
                map.put("fantastic", "差");
            }
            data.add(map);
        }
//        List<List<String>> data = new LinkedList<>();
//        LocalDateTime time = LocalDateTime.now();
//        List<UserParse> dataList = DjsDao.selectUserParse(userId,time);
//        for (UserParse userParse : dataList) {
//            List<String> list = new LinkedList<>();
//            list.add(userParse.getId());
//            list.add(userParse.getUserAccount());
//            list.add(userParse.getStadiumName());
//            list.add(userParse.getStartTime());
//            list.add(userParse.getEndTime());
//            if (userParse.getFantastic() == 0) {
//                list.add("优");
//            } else {
//                list.add("差");
//            }
//            data.add(list);
//        }
        return data;
    }

    @Override
    public List<Map<String,String>> updatePopular(Integer userId, String orderId, String parse) {
        List<Map<String,String>> data = new LinkedList<>();
        int praiseOption = 1;
        if (parse.equals("优")){
            praiseOption = 0;
        }
        int result = DjsDao.updateUserParse(orderId, praiseOption);
        if (result == 1){
            data = getPrevious(userId);
            return data;
        }
        return data;
    }

    /**
     * 好评率计算方法
     *
     * @param a
     * @param b
     * @return
     */
    public double popular(Integer a, Integer b) {
        return a.doubleValue() / b.doubleValue();
    }
}
